package com.burgeon.backend.SHOPBACKEND.rest.find;
/**
 * 员工条件搜索
 */

import com.burgeon.backend.Rest.base.RESTBase;
import com.burgeon.backend.SHOPBACKEND.entity.Employee;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

public class FindEmp extends RESTBase {

    private String ip;
    private String account;
    private String password;

    public FindEmp(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     *
     * @return
     */

    public ArrayList<Employee> getData(String field,String message) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";
        String str = RESULT(Data(field,message), url);

        String code = new JSONArray(str).getJSONObject(0).getString("code");
        if (!"0".equals(code)) {
            throw new Exception(str);
        }

        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
        ArrayList<Employee> employees = new ArrayList<Employee>();
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONArray array = jsonArray.getJSONArray(i);
            Employee employee = new Employee();
            employee.setId(array.getString(0));

            employee.setNo(array.getString(1));
            employee.setIsSaler(array.getString(2));
            employee.setName(array.getString(3));
            employee.setBirthday(array.getString(4));
            employee.setPhone(array.getString(5));
            employee.setState(array.getString(6));
            employee.setBeginDate(array.getString(7));
            employee.setSex(array.getString(8));
            employee.setStore(array.getString(9));
            employee.setIsInvite(array.getString(10));
            employees.add(employee);
        }

        return employees;
    }

    private MultiValueMap Data(String field,String message) {
        HashMap params = new HashMap();
        ArrayList columns = new ArrayList();//查询字段

        columns.add("ID");
        columns.add("NO");
        columns.add("ISSALER");//是否营业员
        columns.add("NAME");
        columns.add("BIRTHDATE");
        columns.add("PHONE");
        columns.add("INCUMBENCY_STS");//在职状况
        columns.add("BEGIN_DATE");//入职日期
        columns.add("SEX");
        columns.add("'C_STORE_ID;NAME'");
        columns.add("IS_WECHAT");

        params.put("columns", columns);
        params.put("table", "C_V_EMPLOYEE");//表名

        params.put("count", "true");


        HashMap paramss = new HashMap();//DOCNO  (AK)	单据编号ISACTIVE
        HashMap expr1 = new HashMap();

        expr1.put("column", "ISACTIVE");//积分抵扣运费需要积分
        expr1.put("condition", "Y");

        HashMap expr2 = new HashMap();

        expr2.put("column","'"+field+"'");
        expr2.put("condition","'"+message+"'");

        paramss.put("combine", "and");//关系
        paramss.put("expr1",expr1);
        paramss.put("expr2",expr2);

        params.put("params", paramss);

        return DATA("Query", params, account, password);
    }

    public static void main(String[] args) {
        FindEmp querryYCDisStatus = new FindEmp("59.110.28.94:90", "nea@burgeon.com.cn", "burgeon55321");
        try {
            System.err.println(querryYCDisStatus.getData("","刘宝苹"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
